//
//  SinaVisitorLoginView.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/13.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit
import SnapKit
//MARK: -
//MARK: - 代理
@objc protocol VisitorViewDelegate:NSObjectProtocol{
    
    @objc optional func Login()//用户将要登录
    @objc optional func Register()//用户将要注册
}


class SinaVisitorLoginView: UIView {
    
    
    
    
    //声明代理属性,代理要用weak修饰,避免循环引用
   weak var delegate:VisitorViewDelegate?
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -
    //MARK: - -----外部设置------
    
    /// 外部设置登录视图信息
    ///
    /// - parameter title:     提示信息
    /// - parameter imageName: 图片名称
    /// - parameter isHome:    是否是主页
    func updateInfo(title:String,imageName:String,isHome:Bool = false){
        
        self.tipLbl.text = title
        self.circleView.image = UIImage(named: imageName)
        if isHome {//是首页
            //执行动画
            startAnimation()
        }else{//不是首页,隐藏房子和遮罩
            
            self.houseView.isHidden = true
            self.backView.isHidden = true
        }
    }
    //MARK: -
    //MARK: - 动画
    private func startAnimation(){
        
        let animator = CABasicAnimation(keyPath: "transform.rotation")
        animator.duration = 20
        animator.repeatCount = MAXFLOAT
        animator.toValue = M_PI * 2
        animator.isRemovedOnCompletion = false//动画完成后不移除(跳转页面,动画也不会停)
        circleView.layer.add(animator, forKey: nil)
    }
    //MARK: -
    //MARK: - -----设置视图------
    override init(frame:CGRect){
        super.init(frame:frame)
        setupUI()
        
    }
    //MARK: -
    //MARK: - 搭建UI界面
    private func setupUI(){
        //设置视图背景色
        self.backgroundColor = UIColor(red: 237.0/255, green: 237.0/255, blue: 237.0/255, alpha: 1)
        
        
        addSubview(circleView)//添加圈圈
        addSubview(backView)//添加遮罩
        addSubview(houseView)//添加房子
        addSubview(tipLbl)//添加提示信息lbl
        addSubview(loginBtn)//添加登录按钮
        loginBtn.addTarget(self, action: #selector(loginClick), for: .touchUpInside)
        addSubview(registerBtn)//添加注册按钮
        registerBtn.addTarget(self, action: #selector(registerClick), for: .touchUpInside)
        setContraint()//设置控件约束
        
    }
    //MARK: -
    //MARK: - 点击登录
   @objc private func loginClick(){
    
        delegate?.Login?()//如果代理的方法时用optional修饰的方法可能没有被实现,需要用?可选解包
        
    }
    //MARK: -
    //MARK: - 点击注册
   @objc private func registerClick(){
    delegate?.Register?()
    
    }
    //MARK: -
    //MARK: - 实例化子控件
    //圈圈
    private lazy var circleView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var backView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //大房子
    private lazy var houseView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //提示信息
    private lazy var tipLbl:UILabel = {
        
        let l = UILabel(title: "关注一些人,回到这里看看有什么惊喜", fontSize: 14, textColor: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
        //设置行数和宽度
        l.numberOfLines = 0
        l.textAlignment = .center
        /*
         注意:设置文字换行:
         1.不能设置左右边距来限制宽度
         2,不能设置左右:在计算非常精确的文本自动换行时,会导致高度计算的不准确
         */
        l.preferredMaxLayoutWidth = 200
        
        return l
    }()
    //登录按钮
    private lazy var loginBtn:UIButton = {
        
        let btn = UIButton(title: "登录", fontSize: 14, textColor: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    //注册按钮
    private lazy var registerBtn:UIButton = {
        
        let btn = UIButton(title: "注册", fontSize: 14, textColor: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1))
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    
    //MARK: -
    //MARK: - 控件约束
    private func setContraint(){
        
        //圈圈
        circleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        //遮罩
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //房子
        houseView.snp.makeConstraints { (make) in
            make.center.equalTo(circleView)
        }
        //提示信息
        tipLbl.snp.makeConstraints { (make) in
            make.top.equalTo(circleView.snp.bottom).offset(20)
            make.centerX.equalTo(circleView)
        }
        //登录按钮
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(tipLbl.snp.bottom).offset(20)
            make.left.equalTo(tipLbl)
            make.width.equalTo(60)
        }
        //注册按钮
        registerBtn.snp.makeConstraints { (make) in
            make.top.size.equalTo(loginBtn)
            make.right.equalTo(tipLbl)
        }
    }
    
    
    
}
